import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.evidencer.java.utils.Printer;

/**
 * @Title: HttpServletDemo.java
 * @Package 
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2014/01/17 8:32:03
 * @version V1.0
 */
@WebServlet(name="RequestDemo",urlPatterns="/RequestDemo")
public class RequestDemo extends HttpServlet {

	/**
	*Title: doGet
	*Description: 
	* @param req
	* @param resp
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		 String encoding = "Shift_Jis";
		 String param1key = "name=";
		 String param1Value = "z&z&z&&zパラメータ";
		 String parem2Key = "password=";
		 String param2Value = "pas&sw&dクラス概要";
		 String parameters = param1key + URLEncoder.encode(param1Value, encoding) + "&" + parem2Key + URLEncoder.encode(param2Value, encoding);
         Printer.sop(parameters,"parameters");

		req.setCharacterEncoding(encoding);
		InputStream in=req.getInputStream();
		Printer.sop(in,"in");
		int len=0;
		byte buf[]=new byte[1024];
		while((len=in.read(buf))!=-1){
			String requestStr=new String(buf,0,len);
			String keyValues[]=requestStr.split("&");
			for(String keyValue : keyValues){
				String singleKeyValues[]=keyValue.split("=");
				Printer.sop(URLDecoder.decode(singleKeyValues[0], encoding)+"="+URLDecoder.decode(singleKeyValues[1], encoding),"param");
			}
			Printer.sop(requestStr,"requestStr");
		}

		
		Map<String,String[]> map=req.getParameterMap();
		Set<Map.Entry<String, String[]>> entrys=map.entrySet();
		Iterator<Map.Entry<String, String[]>> it=entrys.iterator();
		while(it.hasNext()){
			Map.Entry<String, String[]> entry=it.next();
			String name=entry.getKey();
			String[] values=entry.getValue();
			Printer.sop(values.length,"values.length");
			Printer.sop(name+"="+values[0],"name=values[0]");
		}
		
		//         name=z%26z%26z%26%26z%83%70%83%89%83%81%81%5B%83%5E&password=pas%26sw%26d%83%4E%83%89%83%58%8A%54%97%76
		//Sender   name=z%26z%26z%26%26z%83p%83%89%83%81%81%5b%83%5e&password=pas%26sw%26d%83N%83%89%83X%8aT%97v
		// name=z&z&z&&z�p�����[�^&password=pas&sw&d�N���X�T�v
		
		//Receiver name=z%26z%26z%26%26z%83p%83%89%83%81%81%5b%83%5e&password=pas%26sw%26d%83N%83%89%83X%8aT%97v
		// name=z&z&z&&z�pラメータ&password=pas&sw&d�Nラ�X�T�v
		// name=z&z&z&&z�pラメータpassword=pas&sw&d�Nラ�X�T�v
//		BufferedReader in = new BufferedReader(new InputStreamReader(req.getInputStream()));
//		Printer.sop(in,"in");
//		try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}
//		String inputLine="@";
//		while (!(inputLine = in.readLine()).equals(""))
//		    System.out.println(inputLine);
//		in.close();
	}

	/**
	*Title: doPost
	*Description: 
	* @param req
	* @param resp
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doPost(req, resp);
		this.doGet(req, resp);
	}

	/**
	*Title: doOptions
	*Description: 
	* @param req
	* @param resp
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doOptions(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	
	@Override
	protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doOptions(req, resp);
	}

	/**
	*Title: doTrace
	*Description: 
	* @param req
	* @param resp
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doTrace(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	
	@Override
	protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doTrace(req, resp);
	}

	/**
	*Title: service
	*Description: 
	* @param req
	* @param resp
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(req, resp);
	}

}

